package com.example.starterdemo;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.StandardCopyOption;

/**
 * @description:
 * @author: dengPing.xu
 * @date: 2022-07-19
 */
@Slf4j
public class FileUtils {

    public static void main(String[] args) throws IOException {
        String filePath = "D:\\Desktop\\极客时间";
        String copyFilePath = "D:\\Desktop\\极客时间-pdf";


        File file = FileUtil.file(filePath);
        if(!file.exists() || !file.isDirectory()) {
            log.error("文件夹不存在");
            return ;
        }

        File fileCopy = FileUtil.file(copyFilePath);
        if(!fileCopy.exists() || !fileCopy.isDirectory()) {
            log.info("复制文件夹不存在");
            fileCopy.mkdirs();
        }

        extractedFile(file);


    }

    private static void extractedFile(File file) {
        File[] files = file.listFiles();
        for (File item : files) {
            String replacePath = item.getAbsolutePath().replace("极客时间", "极客时间-pdf");
            if(item.isDirectory()) {
                log.info("正在处理文件夹：{}" , file.getName());
                FileUtil.mkdir(replacePath);
                extractedFile(item);
            }else if(item.getAbsolutePath().contains(".html")) {
                String name = item.getName();
                File copyFile = FileUtil.touch(replacePath);
                FileUtil.copyFile(item, copyFile, StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }
}
